﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using BAL;
using Common;
using System.Text;
namespace Web
{
    public partial class ChartScoreDiffic : System.Web.UI.Page
    {
        protected int examroomid = 0, studentid = 0;
        protected string piechart = "", barchart = "";
        protected bool isquery = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserValidate.CheckLogin(this.Request, "StatistScore"))
            {
                if (!UserValidate.CheckQuery())
                {
                    Response.Write("<script type=\"text/javascript\">top.location.href='login.aspx';</script>");
                    Response.Redirect("login.aspx");
                    Response.End();
                }
                else
                {
                    isquery = true;
                }
            }
            if (isquery)
            {
                studentid = int.Parse(HttpContext.Current.Session["QueryStudentId"] as string);
            }
            else if (!PageValidate.QueryStringIsEmpty("studentid"))
            {
                try
                {
                    studentid = int.Parse(PageValidate.QueryString("studentid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!PageValidate.QueryStringIsEmpty("examroomid"))
            {
                try
                {
                    examroomid = int.Parse(PageValidate.QueryString("examroomid"));
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                    Response.End();
                }
            }
            if (!IsPostBack)
            {
                if (examroomid > 0 && studentid > 0)
                {
                    IList<StudentScoreExtendInfo> infos = new List<StudentScoreExtendInfo>();
                    infos = StudentScoreExtend.GetList(examroomid, 0, 0, 0, 0, 0, 0, 0);
                    StudentScoreExtendInfo myInfo = infos.Single(t => t.StudentId == studentid);
                    IList<DifficInfo> listDiffic = GetDiffic();
                    IList<QuestionInfo> listQuestion = GetQuestion(myInfo.ExamPaperId);

                    StringBuilder sb = new StringBuilder();
                    foreach (DifficInfo diffic in listDiffic)
                    {
                        decimal questScoreCount = 0;
                        IList<int> questIds = new List<int>();
                        foreach (QuestionInfo quest in listQuestion)
                        {
                            if (quest.DifficId == diffic.Id)
                            {
                                questScoreCount += quest.Score;
                                questIds.Add(quest.Id);
                            }
                        }
                        if (questIds.Count == 0)
                        {
                            continue;
                        }
                        piechart += "['" + diffic.Name + "'," + questIds.Count + "],";
                        string[] arrScore = GetClassScoreByQuest(infos, myInfo.StudentId, myInfo.ClassId, myInfo.GradeId, myInfo.AreaId, questIds);
                        string percentMy = "0";
                        string percentClass = "0";
                        string percentGrade = "0";
                        string percentArea = "0";
                        if (arrScore[0] != "0")
                        {
                            percentMy = Math.Round(decimal.Parse(arrScore[0]) / questScoreCount * 100).ToString() + "%";
                        }
                        if (arrScore[1] != "0")
                        {
                            percentClass = Math.Round(decimal.Parse(arrScore[1]) / (questScoreCount * int.Parse(arrScore[4])) * 100).ToString() + "%";
                        }
                        if (arrScore[2] != "0")
                        {
                            percentGrade = Math.Round(decimal.Parse(arrScore[2]) / (questScoreCount * int.Parse(arrScore[5])) * 100).ToString() + "%";
                        }
                        if (arrScore[3] != "0")
                        {
                            percentArea = Math.Round(decimal.Parse(arrScore[3]) / (questScoreCount * int.Parse(arrScore[6])) * 100).ToString() + "%";
                        }
                        barchart += "['" + diffic.Name + "'," + percentMy.Replace("%", "") + "],";
                        sb.Append("<tr>");
                        sb.Append("<td align='left'>");
                        sb.Append(diffic.Name);
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(questIds.Count.ToString());
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(questScoreCount.ToString());
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(arrScore[0]);
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(percentMy);
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(percentClass);
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(percentGrade);
                        sb.Append("</td>");
                        sb.Append("<td>");
                        sb.Append(percentArea);
                        sb.Append("</td>");
                        sb.Append("</tr>");
                    }
                    this.DifficList.InnerHtml = sb.ToString();
                    piechart = PageValidate.IsEmpty(piechart) ? "" : piechart.Substring(0, piechart.Length - 1);
                    barchart = PageValidate.IsEmpty(barchart) ? "" : barchart.Substring(0, barchart.Length - 1);
                }
            }
        }
        private string[] GetClassScoreByQuest(IList<StudentScoreExtendInfo> infos, int sid, int classid, int gradeid, int areaid, IList<int> questid)
        {
            decimal myScore = 0, classScore = 0, gradeScore = 0, areaScore = 0;
            int classNum = 0, gradeNum = 0, areaNum = 0;
            foreach (StudentScoreExtendInfo info in infos)
            {
                IList<ScoreInfo> scoreInfos = ScoreHelper.GetScoreList(info.QuestItemScore);
                scoreInfos = scoreInfos.Where(t => GetArray<int>.Get(questid).Contains(t.Id)).ToList();
                decimal score = 0;
                foreach (ScoreInfo scoreInfo in scoreInfos)
                {
                    if (!PageValidate.IsEmpty(scoreInfo.Score))
                    {
                        score += decimal.Parse(scoreInfo.Score);
                    }
                }
                if (info.StudentId == sid)
                {
                    myScore = score;
                }
                if (info.ClassId == classid)
                {
                    classScore += score;
                    classNum++;
                }
                if (info.GradeId == gradeid)
                {
                    gradeScore += score;
                    gradeNum++;
                }
                if (info.AreaId == areaid)
                {
                    areaScore += score;
                    areaNum++;
                }
            }
            return new string[] { myScore.ToString(), classScore.ToString(), gradeScore.ToString(), areaScore.ToString(), classNum.ToString(), gradeNum.ToString(), areaNum.ToString() };
        }
        private IList<DifficInfo> GetDiffic()
        {
            IList<DifficInfo> infos = new List<DifficInfo>();
            infos = Diffic.GetDifficInfos(0, 0);
            return infos;
        }

        private IList<QuestionInfo> GetQuestion(int exampaperid)
        {
            IList<QuestionInfo> infos = new List<QuestionInfo>();
            infos = Question.GetQuestions(exampaperid, "", "", "", "", "", "", "", "", "", 0, 0);
            return infos;
        }
    }
}
